<!-- 时间轴标尺组件 -->
<template>
  <div class="timeline-ruler" @mousedown="onRulerClick">
    <div 
      class="ruler-inner" 
      :style="{ width: `${totalDuration * zoom}px` }"
    >
      <div 
        v-for="tick in ticks" 
        :key="tick" 
        class="ruler-tick"
        :class="{ 'major': tick % 10 === 0 }"
        :style="{ left: `${tick * zoom / 10}px` }"
      >
        <span v-if="tick % 10 === 0" class="tick-label">
          {{ formatTime(tick / 10) }}
        </span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, defineProps, defineEmits } from 'vue'

// 定义属性
const props = defineProps({
  totalDuration: {
    type: Number,
    required: true
  },
  zoom: {
    type: Number,
    default: 10
  }
})

// 定义事件
const emit = defineEmits(['seek'])

// 计算刻度
const ticks = computed(() => {
  const result: number[] = []
  for (let i = 0; i <= props.totalDuration * 10; i++) {
    result.push(i)
  }
  return result
})

// 格式化时间
const formatTime = (seconds: number) => {
  const mins = Math.floor(seconds / 60)
  const secs = Math.floor(seconds % 60)
  return `${mins}:${secs.toString().padStart(2, '0')}`
}

// 点击标尺跳转到对应时间
const onRulerClick = (event: MouseEvent) => {
  const rect = (event.currentTarget as HTMLElement).getBoundingClientRect()
  const x = event.clientX - rect.left
  const time = x / props.zoom
  emit('seek', time)
}
</script>

<style lang="scss" scoped>
.timeline-ruler {
  height: 24px;
  background-color: var(--card-color);
  border-bottom: 1px solid var(--border-color-base);
  position: relative;
  overflow: hidden;
  cursor: pointer;
  
  .ruler-inner {
    height: 100%;
    position: relative;
    min-width: 100%;
    
    .ruler-tick {
      position: absolute;
      top: 0;
      height: 100%;
      width: 1px;
      background-color: var(--border-color-base);
      
      &.major {
        background-color: var(--text-color-secondary);
      }
      
      .tick-label {
        position: absolute;
        top: 4px;
        left: 4px;
        font-size: 10px;
        color: var(--text-color-secondary);
        white-space: nowrap;
      }
    }
  }
}
</style> 